package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.AchievementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/achievement")
public class AchievementController {

    @Autowired
    private AchievementService achievementService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 业绩结算-返润流水
     * @param receiveName
     * @param receivePhone
     * @param payName
     * @param payPhone
     * @param ifParent
     * @param type
     * @param keywords
     * @param startTime
     * @param endTime
     * @param status
     * @param pageIndex
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("/list")
    public PageInfo list(
            @RequestParam(value = "receiveName", required = false) String receiveName,
            @RequestParam(value = "receivePhone", required = false) String receivePhone,
            @RequestParam(value = "payName", required = false) String payName,
            @RequestParam(value = "payPhone", required = false) String payPhone,
            @RequestParam(value = "ifParent", required = false,defaultValue = "-1") Integer ifParent,
            @RequestParam(value = "type", required = false,defaultValue = "-1") Integer type,
            @RequestParam(value = "keywords", required = false) String keywords,
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false) String endTime,
            @RequestParam(value = "status", required = false,defaultValue = "-1") Integer status,
            @RequestParam(value = "pageIndex", required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize", required = false,defaultValue = "10") Integer pageSize,
            HttpServletRequest request
    )
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>();
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("receiveName",receiveName);
        param.put("receivePhone",receivePhone);
        param.put("payName",payName);
        param.put("payPhone",payPhone);
        param.put("ifParent",ifParent);
        param.put("type",type);
        param.put("keywords",keywords);
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        param.put("status",status);

        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(achievementService.list(param));
    }

    /**
     * 业绩结算-返润流水 导出
     * @param receiveName
     * @param receivePhone
     * @param payName
     * @param payPhone
     * @param ifParent
     * @param type
     * @param keywords
     * @param startTime
     * @param endTime
     * @param status
     * @param session
     * @param response
     * @throws Exception
     */
    @GetMapping("/exportlist")
    public void exportList(
            @RequestParam(value = "receiveName", required = false) String receiveName,
            @RequestParam(value = "receivePhone", required = false) String receivePhone,
            @RequestParam(value = "payName", required = false) String payName,
            @RequestParam(value = "payPhone", required = false) String payPhone,
            @RequestParam(value = "ifParent", required = false,defaultValue = "-1") Integer ifParent,
            @RequestParam(value = "type", required = false,defaultValue = "-1") Integer type,
            @RequestParam(value = "keywords", required = false) String keywords,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "status", required = false,defaultValue = "-1") Integer status,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>();
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("receiveName",receiveName);
        param.put("receivePhone",receivePhone);
        param.put("payName",payName);
        param.put("payPhone",payPhone);
        param.put("ifParent",ifParent);
        param.put("type",type);
        param.put("keywords",keywords);
        endTime = StringUtils.isEmpty(endTime)? new SimpleDateFormat("yyyy-MM-dd").format(new Date()) :endTime;
        if(StringUtils.isEmpty(startTime)){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, -30);
            Date d = c.getTime();
            startTime = new SimpleDateFormat("yyyy-MM-dd").format(d);
        }
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        param.put("status",status);
        achievementService.exportList(param,response);
    }


    /**
     *  业绩报表
     * @param startTime
     * @param endTime
     * @param name
     * @param phone
     * @param pageIndex
     * @param pageSize
     * @param session
     * @return
     */
    @GetMapping("/report")
    public PageInfo report(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "pageIndex", required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize", required = false,defaultValue = "10") Integer pageSize,
            HttpServletRequest request
    )
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>();
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("name",name);
        param.put("phone",phone);
        endTime = StringUtils.isEmpty(endTime)? new SimpleDateFormat("yyyy-MM-dd").format(new Date()) :endTime;
        if(StringUtils.isEmpty(startTime)){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, -30);
            Date d = c.getTime();
            startTime = new SimpleDateFormat("yyyy-MM-dd").format(d);
        }
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(achievementService.report(param));
    }

    /**
     * 业绩报表导出
     * @param startTime
     * @param endTime
     * @param name
     * @param phone
     * @param session
     * @param response
     * @throws Exception
     */
    @GetMapping("/exportreport")
    public void exportReport(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception
    {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String,Object> param = new HashMap<>();
        String orgId = userinfo.getOrgId();
        param.put("orgId",orgId);
        param.put("name",name);
        param.put("phone",phone);
        endTime = StringUtils.isEmpty(endTime)? new SimpleDateFormat("yyyy-MM-dd").format(new Date()) :endTime;
        if(StringUtils.isEmpty(startTime)){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, -30);
            Date d = c.getTime();
            startTime = new SimpleDateFormat("yyyy-MM-dd").format(d);
        }
        param.put("startTime",startTime);
        param.put("endTime",endTime);
        achievementService.exportReport(param,response);
    }
}
